<?php

namespace App\Model\Task;

use App\Constants\TaskReceiveStatus;
use App\Model\Model;
use App\Model\User\User;
use Hyperf\Database\Model\SoftDeletes;
use Yj\Model\UserTrait;

/**
 * @property integer $id
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $delete_time
 * @property integer $user_id
 * @property integer $task_id
 */
class TaskReceive extends Model
{
    use SoftDeletes;
    use UserTrait;


    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'task_receive';

    
    protected $fillable = [
        'create_time',
        'update_time',
        'delete_time',
        'cancel_time',
        'task_price',
        'user_id',
        'tasker_id',
        'status',
        'reason',
        'task_id',
    ];

    protected $appends = [
        'cancel_timestamp',
        'status_format',
        'create_time_format',
    ];


    public function getCreateTimeFormatAttribute()
    {
        return $this->checkAttributes('create_time', function ($value) {
            return date_huan($value);
        });
    }


    protected function getStatusFormatAttribute()
    {
        return $this->checkAttributes('status', function ($status) {
            return TaskReceiveStatus::getMessage($status);
        });
    }

    protected function getCancelTimeStampAttribute()
    {
        return $this->checkAttributes('cancel_time', function ($cancel_time) {
            return max($cancel_time - time(), 0);
        });
    }

    public function Task()
    {
        return $this->hasOne(Task::class, 'id', 'task_id');
    }

    public function TaskReceiveDetail()
    {
        return $this->hasMany(TaskReceiveDetail::class, 'task_receive_id', 'id');
    }

    public function User(){
        return $this->belongsTo(User::class, 'user_id', 'uid');
    }
}
